package sxkyxt.kjyxgs.cn.ishop_gather_uploading_android;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.math.BigDecimal;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @version V1.0 <描述当前版本功能>.
 * @filename: sxkyxt.kjyxgs.cn.ishop_gather_uploading_android
 * @Author HY  on {Administrator}.
 * @Org 山西科远信通科技有限公司.
 * @Description: ${TODO} .
 * @Motto 大梦谁先觉, 贫僧我自知..
 * @Date 2018/11/28
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private LinearLayout mActivityMainShootPhotoViewLinner;
    private LinearLayout mActivityMainGetUploadPhotoViewLinner;
    private TextView mActivityMainGetUploadPhotoCountText;
    private int count;
    private MainBean mainBean;
    private LoginBean loginBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initContent();
    }

    private void initContent() {
        OkHttpEngine.getInstance().getAsynHttp(CONSTS.HOST + CONSTS.REMOTELIST + "?pn=1" + "&sid=" + loginBean.getExtend().getShop().getSid(), this).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, R.string.main_item_access_list_error + "--->" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String result = response.body().string();
                try {
                    mainBean = new Gson().newBuilder().create().fromJson(result, MainBean.class);
                } catch (Exception e) {
                    Log.d(TAG, "onResponse: error " + e.getMessage());
                }
                Log.d(TAG, "onResponse: " + result);
                if (mainBean.getCode() != 100)
                    return;
                if (mainBean.getExtend().getPageinfo().getList() != null)
                    count = mainBean.getExtend().getPageinfo().getList().size();
                Log.d(TAG, "onResponse: count" + count);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mActivityMainGetUploadPhotoCountText.setText(String.valueOf(count));
                    }
                });
            }
        });
    }

    public  String getPrettyNumber(String number) {
        return BigDecimal.valueOf(Double.parseDouble(number))
                .stripTrailingZeros().toPlainString();
    }

    private void initView() {
        mActivityMainShootPhotoViewLinner = (LinearLayout) findViewById(R.id.linner_activity_main_shoot_photo_view);
        mActivityMainShootPhotoViewLinner.setOnClickListener(this);
        mActivityMainGetUploadPhotoViewLinner = (LinearLayout) findViewById(R.id.linner_activity_main_get_upload_photo_view);
        mActivityMainGetUploadPhotoViewLinner.setOnClickListener(this);
        mActivityMainGetUploadPhotoCountText = (TextView) findViewById(R.id.text_activity_main_get_upload_photo_count);
        loginBean = getIntent().getParcelableExtra("loginRequestData");
        Log.d(TAG, "initView: " + loginBean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linner_activity_main_shoot_photo_view:
                Intent intent_upload = new Intent(this, UploadActivity.class);
                intent_upload.putExtra("loginRequestData", loginBean);
                startActivity(intent_upload);
                break;
            case R.id.linner_activity_main_get_upload_photo_view:
                Intent intent = new Intent(this, PhotoActivity.class);
                intent.putExtra("photoData", mainBean);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
